<?php
class CPyramidAccountDetail extends Zend_Db_Table
{
	protected $_name = '';
	protected $_primary = '';
	
	public function __construct()
	{
		$this->_name = R_PRE . 'pyramid_account_detail';
		$this->_primary = 'account_detail_id';
		parent::__construct();
	}
	
	public function add($row)
	{
		return $this->insert($row);
	}
	
	public function getMyAmount($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('facebook_id = ?', $facebookId);
		
		$sql = 'select sum(amount) as c from ' . $this->_name . ' where ' . $where;
		$result = $db->fetchAll($sql);
		
		if (empty($result[0]['c'])) {
			return 0;
		}
		
		return $result[0]['c'];
	}
	
	public function getMyData($begin, $end, $facebookId)
	{
		$begin = time();
		$end = time()-3600*24*31;
		
		$sql = 'select sum(amount) as sum_amount, add_time from ' . $this->_name . ' where add_time >=  ' . $end . ' and add_time <= ' . $begin . ' and facebook_id=' . $facebookId . ' group by facebook_id';
		
		$db = $this->getAdapter();
		return $db->fetchAll($sql);
	}
}